<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 目录树 -->
      <el-col :lg="4" :md="5" :sm="6" :xs="24" class="mb-[12px] !pr-0">
        <databoard-catalog-tree
          v-model="searchParam.catalogId"
          @node-click="search"
        />
      </el-col>
      <el-col :lg="20" :md="19" :sm="18" :xs="24">
        <div v-if="showSearch" class="search-container">
          <CommonSearch
            :formConfig="searchColumns"
            :inline="true"
            labelWidth="auto"
            formItemWidth="auto"
            :model="searchParam"
          >
            <template #after>
              <el-button type="primary" @click="search">
                <i-ep-search />
                搜索
              </el-button>
              <el-button @click="reset">
                <i-ep-refresh />
                重置
              </el-button>
            </template>
          </CommonSearch>
        </div>

        <el-card shadow="never" class="table-container">
          <template #header>
            <el-button type="success" plain @click="openDialog()"
              ><i-ep-plus />新增</el-button
            >
            <el-button
              type="danger"
              plain
              :disabled="ids.length === 0"
              @click="handleTableDelete()"
              ><i-ep-delete />删除</el-button
            >
          </template>

          <el-table
            ref="dataTableRef"
            v-loading="loading"
            :data="tableData"
            highlight-current-row
            border
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55" align="center" />
            <!-- 表格内容操作 -->
            <template v-for="item in tableColumns" :key="item">
              <TableColumn v-if="item.prop && item.isShow" :column="item">
                <template v-for="slot in Object.keys($slots)" #[slot]="scope">
                  <slot :name="slot" v-bind="scope"></slot>
                </template>
              </TableColumn>
            </template>
            <el-table-column
              fixed="right"
              label="操作"
              width="210"
              align="center"
            >
              <template #default="scope">
                <el-button
                  type="primary"
                  size="small"
                  link
                  @click="openDialogdetail(scope.row)"
                  ><i-ep-search />
                  查看
                </el-button>
                <el-button
                  type="primary"
                  size="small"
                  link
                  @click="openDialog(scope.row)"
                  ><i-ep-edit />编辑</el-button
                >
                <el-button
                  type="primary"
                  size="small"
                  link
                  @click="handleTableDelete(scope.row.id)"
                  ><i-ep-delete />删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <el-pagination
            :background="true"
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :total="pageable.total"
            layout="total, sizes, prev, pager, next, jumper"
            class="mt-[20px]"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-card>
      </el-col>
    </el-row>

    <sysdataboradinfo-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id: string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
    <!-- 列设置 抽屉组件 -->
    <ColSetting
      v-model:col-setting="tableColumns"
      v-model:search-setting="searchColumns"
      @search-handle="(e: boolean) => (showSearch = e)"
    />
  </div>
</template>

<script setup lang="tsx">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "SYSTEMDATABOARDMANAGE",
  inheritAttrs: false,
});

/** 自定义设置方法   start **/

// 搜索参数 设置为响应式
const searchColumns: any = reactive([
  {
    type: "input", // 普通输入框
    isShow: true,
    inputType: "text", // 类型, text/textarea
    label: "看板编号", // 标签文本
    fieldName: "databoradCode", // 字段名称
    placeholder: "请输入看板编号", // 提示文字
    initValue: "", // 默认值
  },
  {
    type: "input", // 普通输入框
    isShow: true,
    inputType: "text", // 类型, text/textarea
    label: "看板名称", // 标签文本
    fieldName: "databoradName", // 字段名称
    placeholder: "请输入看板名称", // 提示文字
    initValue: "", // 默认值
  },
  // {
  //   type: "input", // 普通输入框
  //   isShow: true,
  //   inputType: "text", // 类型, text/textarea
  //   label: "目录主键", // 标签文本
  //   fieldName: "catalogId", // 字段名称
  //   placeholder: "请输入目录主键", // 提示文字
  //   initValue: "", // 默认值
  // },
  {
    type: "select",
    label: "看板类型",
    isShow: true,
    fieldName: "databoradType",
    placeholder: "请选择看板类型",
    initValue: null,
    defaultFirstOption: true,
    optionList: useDictionary("DataBoardType"),
  },
]);

// 接收 columns 并设置为响应式
const tableColumns = reactive([
  { prop: "databoradCode", label: "看板编号", isShow: true, minWidth: "130" },
  { prop: "databoradName", label: "看板名称", isShow: true, minWidth: "130" },
  {
    prop: "catalogName",
    label: "目录名称",
    isShow: true,
    minWidth: "130",
  },
  // { prop: "catalogId", label: "目录主键", isShow: true, minWidth: "100" },
  {
    prop: "databoradType",
    label: "看板类型",
    width: 120,
    isShow: true,
    render: (scope: any) =>
      scope.row.databoradType !== "1" ? (
        <el-tag type="warning">
          {useDictionary("DataBoardType", scope.row.databoradType)}
        </el-tag>
      ) : (
        <el-tag>
          {useDictionary("DataBoardType", scope.row.databoradType)}
        </el-tag>
      ),
  },
  // {
  //   prop: "databoradUrl",
  //   label: "看板地址",
  //   isShow: true,
  // },
  {
    prop: "enabled",
    label: "是否启用",
    width: 120,
    isShow: true,
    render: (scope: any) =>
      scope.row.enabled !== 1 ? (
        <el-tag type="info">
          {useDictionary("Enabled", scope.row.enabled)}
        </el-tag>
      ) : (
        <el-tag type="success">
          {useDictionary("Enabled", scope.row.enabled)}
        </el-tag>
      ),
  },
  // {
  //   prop: "remarks",
  //   label: "备注",
  //   isShow: true,
  //   minWidth: "100",
  //   render: (scope: any) => <span>{scope.row.remarks || "--"}</span>,
  // },
  { prop: "lastUpdateTime", label: "更新时间", isShow: true, minWidth: "100" },
  // {
  //   prop: "lastUpdatedUserName",
  //   label: "更新人姓名",
  //   isShow: true,
  //   minWidth: "100",
  // },
]);

// 是否显示搜索框
const showSearch = ref(true);

/** 列表项自定义方法   end **/

// 请求参数
const requestApi = "/system/databoradinfo/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  databoradCode: "",
  databoradName: "",
  catalogId: "",
  databoradType: "",
};

const deleteParam = {
  api: "/system/databoradinfo/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/system/databoradinfo/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开数据看板表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改数据看板";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增数据看板";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开数据看板表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "数据看板详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
</script>
<style lang="scss" scoped>
@media only screen and (max-width: 768px) {
  .el-col-xs-24 {
    padding-right: 10px !important;
  }
}
</style>
